package com.skyline.attraction.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.skyline.attraction.model.Attraction;
import com.skyline.attraction.type.AttractionType;
import com.skyline.base.type.Activity;

public class AttractionMapper implements RowMapper<Attraction> {

	private final static String COLUMN_ID = "id";
	private final static String COLUMN_NAME = "name";
	private final static String COLUMN_PORTRAIT = "portrait";
	private final static String COLUMN_CATEGORY = "category";

	private final static String COLUMN_CREATORID = "creatorId";
	private final static String COLUMN_CREATORNICKNAME = "creatorNickname";
	private final static String COLUMN_CREATOREMAIL = "creatorEmail";
	private final static String COLUMN_CREATORPORTRAIT = "creatorPortrait";

	private final static String COLUMN_ADMINID = "adminId";
	private final static String COLUMN_ADMINNICKNAME = "adminNickname";
	private final static String COLUMN_ADMINEMAIL = "adminEmail";
	private final static String COLUMN_ADMINPORTRAIT = "adminPortrait";

	private final static String COLUMN_CREATETIME = "createTime";
	private final static String COLUMN_UPDATETIME = "updateTime";

	private final static String COLUMN_INTRODUCTION = "introduction";

	private final static String COLUMN_COUNTRY = "country";
	private final static String COLUMN_PROVINCE = "province";
	private final static String COLUMN_CITY = "city";
	private final static String COLUMN_SITEID = "siteId";

	private final static String COLUMN_DETAILADDRESS = "detailAddress";
	private final static String COLUMN_LATITUDE = "latitude";
	private final static String COLUMN_LONGITUDE = "longitude";

	private final static String COLUMN_VISTNUM = "visitNum";
	private final static String COLUMN_STAR = "star";
	private final static String COLUMN_VOTES = "votes";

	private final static String COLUMN_ACTIVITY = "activity";

	private final static AttractionMapper MAPPER = new AttractionMapper();

	public static AttractionMapper getMapper() {
		return MAPPER;
	}

	public Attraction mapRow(ResultSet rs, int rowNum) throws SQLException {
		Attraction attraction = new Attraction();

		attraction.setId(rs.getLong(COLUMN_ID));
		attraction.setName(rs.getString(COLUMN_NAME));
		attraction.setPortrait(rs.getString(COLUMN_PORTRAIT));
		attraction.setCategory(AttractionType.valueOf(rs.getString(COLUMN_CATEGORY)));

		attraction.setCreatorId(rs.getLong(COLUMN_CREATORID));
		attraction.setCreatorNickname(rs.getString(COLUMN_CREATORNICKNAME));
		attraction.setCreatorEmail(rs.getString(COLUMN_CREATOREMAIL));
		attraction.setCreatorPortrait(rs.getString(COLUMN_CREATORPORTRAIT));

		attraction.setAdminId(rs.getLong(COLUMN_ADMINID));
		attraction.setAdminNickname(rs.getString(COLUMN_ADMINNICKNAME));
		attraction.setAdminEmail(rs.getString(COLUMN_ADMINEMAIL));
		attraction.setAdminPortrait(rs.getString(COLUMN_ADMINPORTRAIT));
		
		attraction.setCountry(rs.getString(COLUMN_COUNTRY));
		attraction.setProvince(rs.getString(COLUMN_PROVINCE));
		attraction.setCity(rs.getString(COLUMN_CITY));
		attraction.setSiteId(rs.getString(COLUMN_SITEID));

		attraction.setDetailAddress(rs.getString(COLUMN_DETAILADDRESS));
		
		attraction.setCreateTime(rs.getDate(COLUMN_CREATETIME));
		attraction.setUpdateTime(rs.getDate(COLUMN_UPDATETIME));
		
		attraction.setLatitude(rs.getDouble(COLUMN_LATITUDE));
		attraction.setLongitude(rs.getDouble(COLUMN_LONGITUDE));
		
		attraction.setStar(rs.getFloat(COLUMN_STAR));
		attraction.setVotes(rs.getInt(COLUMN_VOTES));
		attraction.setVisitNum(rs.getInt(COLUMN_VISTNUM));
		attraction.setActivity(Activity.valueOf((rs.getString(COLUMN_ACTIVITY))));

		try {
			attraction.setIntroduction(rs.getString(COLUMN_INTRODUCTION));
		} catch (Exception e) {
			attraction.setIntroduction(null);
		}
		
		return attraction;
	}
}
